package com.yamed.bus.web;

import com.yamed.bus.dal.entity.PromoteApkConfig;
import com.yamed.bus.service.PromoteApkService;
import com.yamed.bus.web.vo.ResponseVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/promote")
public class PromoteApkController {

    @Resource
    private PromoteApkService promoteApkService;
    @Resource
    private HttpServletRequest httpServletRequest;

    /**
     * 新增
     *
     * @param promoteApkConfig
     * @return
     */
    @PostMapping("/ald_sea_product_admin/addAppConfig")
    public ResponseVO addAppConfig(@RequestBody PromoteApkConfig promoteApkConfig) {
        promoteApkService.addAppConfig(promoteApkConfig);
        return ResponseVO.success();
    }

    /**
     * 更新配置
     *
     * @param promoteApkConfig
     * @return
     */
    @PostMapping("/ald_sea_product_admin/updateAppConfig")
    public ResponseVO updateAppConfig(@RequestBody PromoteApkConfig promoteApkConfig) {
        promoteApkService.updateAppConfig(promoteApkConfig);
        return ResponseVO.success();
    }

    /**
     * 查询推广apk 配置
     *
     * @param promoteCode
     * @return
     */
    @GetMapping("/getAppConfig/{promoteCode}")
    public ResponseVO getAppConfig(@PathVariable String promoteCode) {
        try {
            String ip = getIpAddr();
            PromoteApkConfig appConfig = promoteApkService.getPromoteAppConfig(promoteCode, ip);
            return ResponseVO.success(appConfig);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseVO.error(e.getMessage());
        }

    }

    /**
     * 更新下载次数
     *
     * @param id
     * @return
     */
    @GetMapping("/clickApkUrl/{id}")
    public ResponseVO clickApkUrl(@PathVariable("id") Long id) {
        String ip = httpServletRequest.getRemoteAddr();
        promoteApkService.updateDownloadCount(id, ip);
        return ResponseVO.success();
    }

    /**
     * 查看链接的配置情况
     *
     * @param promoteCode
     * @return
     */
    @GetMapping("/ald_sea_product_admin/promoteInfo/{promoteCode}")
    public ResponseVO promoteInfo(@PathVariable String promoteCode) {
        promoteCode = Objects.isNull(promoteCode) || "all".equals(promoteCode) ? "" : promoteCode;
        return ResponseVO.success(promoteApkService.getList(promoteCode));
    }

    /**
     * 查看链接的统计转化情况
     *
     * @param promoteCode
     * @return
     */
    @GetMapping("/ald_sea_product_admin/promoteCount/{promoteCode}")
    public ResponseVO promoteCount(@PathVariable String promoteCode) {
        promoteCode = Objects.isNull(promoteCode) || "all".equals(promoteCode) ? "" : promoteCode;
        return ResponseVO.success(promoteApkService.getTransList(promoteCode));
    }

    /**
     * 获取实际ip地址
     * @return ip地址
     */
    private String getIpAddr() {
        String ip = httpServletRequest.getHeader("x-forwarded-for");
        // 多次反向代理后会有多个ip值，第一个ip才是真实ip
        if (ip != null && !"".equals(ip) && ip.indexOf(",") != -1) {
            ip = ip.split(",")[0];
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = httpServletRequest.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = httpServletRequest.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = httpServletRequest.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = httpServletRequest.getRemoteAddr();
        }
        return ip;
    }
}

